import axios from 'axios'
import type { Product, BaseResponse, PaginationResponse } from '../types'

// 获取产品列表
export const getProducts = (page = 1, limit = 10): Promise<PaginationResponse<Product>> => {
  return axios.get('/api/products', {
    params: { page, limit },
  })
}

// 获取单个产品
export const getProductById = (id: number | string): Promise<BaseResponse<Product>> => {
  return axios.get(`/api/products/${id}`)
}

// 创建产品
export const createProduct = (productData: Omit<Product, 'id' | 'createTime'>): Promise<BaseResponse<Product>> => {
  return axios.post('/api/products', productData)
}

// 更新产品
export const updateProduct = (id: number | string, productData: Partial<Product>): Promise<BaseResponse<Product>> => {
  return axios.put(`/api/products/${id}`, productData)
}

// 删除产品
export const deleteProduct = (id: number | string): Promise<BaseResponse<{ id: number | string }>> => {
  return axios.delete(`/api/products/${id}`)
}

// 批量获取产品
export const getProductsByIds = (ids: (number | string)[]): Promise<BaseResponse<Product[]>> => {
  return axios.get('/api/products/batch', {
    params: {
      ids: ids.join(','),
    },
  })
}

// 更新产品状态
export const updateProductStatus = (id: number | string, status: 'on' | 'off'): Promise<BaseResponse<Product>> => {
  return axios.patch(`/api/products/${id}/status`, {
    status,
  })
}